/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FileBrowser;

import javax.microedition.lcdui.*;
import java.util.*;


/**
 *
 * @author thomas.s
 */
public class ScreenSaver extends Canvas {

    MediaPlayer pl;
    long lastPress = System.currentTimeMillis();
    int keyPress=0;
    int showInfo=20;
    Timer timer=new Timer();


    ScreenSaver ( MediaPlayer p )
    {
        pl = p;
        timer.schedule(new updateTask(this), 0, 500 );
        setFullScreenMode(true);
    }

    class updateTask extends TimerTask {
        private ScreenSaver canvas;
        int gpxUpdate=0;
        updateTask( ScreenSaver c ){  canvas = c;  }
        public void run()
        {
            if (showInfo>=0) canvas.repaint();
        }
    }


    protected void paint( Graphics g )
    {
        int w = getWidth(), h = getHeight();
        int col = 0xffffff;
        if (showInfo==4) col=0xc0c0c0; else
        if (showInfo==3) col=0x808080; else
        if (showInfo==2) col=0x404040; else
        if (showInfo==1) col=0x202020; else
        if (showInfo<1) col=0;

        
        g.setColor((col&0xff)/5);
        g.fillRect(0,0,w,h);

        showInfo--;
        if (showInfo>0)
        {
            Font f = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL);
            int fh = f.getHeight();
            Calendar c = Calendar.getInstance();
            StringBuffer s = new StringBuffer(40);
            int hh = c.get(Calendar.HOUR_OF_DAY);
            int mm = c.get(Calendar.MINUTE);
            int ss = c.get(Calendar.SECOND);
            s.append(hh/10); s.append(hh%10); s.append(":");
            s.append(mm/10); s.append(mm%10); s.append(":");
            s.append(ss/10); s.append(ss%10);
            String t;
            g.setColor(col);
            t = pl.paused ? "paused" : "playing...";
            g.drawString(t,w/2-f.stringWidth(t)/2,h/2-fh-fh/2,0);

            t = pl.enabled ? pl.curr : "wait";
            g.drawString(t,w/2-f.stringWidth(t)/2,h/2-fh/2,0);

            pl.pos();
            g.drawString(pl.sPos, w/2-f.stringWidth(pl.sPos)/2, h/2+fh/2, 0);

            t = s.toString();
            g.drawString(t, w/2-f.stringWidth(t)/2, 0, 0);

            t = "Memory: " + (int)((Runtime.getRuntime().freeMemory()*100)/Runtime.getRuntime().totalMemory()) + "%";
            g.drawString(t, w/2-f.stringWidth(t)/2, h-fh, 0);

/*
            t = pl.content;
            g.drawString(t, w/2-f.stringWidth(t)/2, h-fh*2, 0);
            t = pl.title;
            g.drawString(t, w/2-f.stringWidth(t)/2, h-fh*3, 0);
            t = pl.interpret;
            g.drawString(t, w/2-f.stringWidth(t)/2, h-fh*4, 0);
*/
        }

    }

    public void pointerPressed( int x, int y )
    {
        pl.vibrate();
        showInfo=30;
    }

    public void pointerDragged( int x, int y )
    {
    }

    public void pointerReleased( int x, int y )
    {
    }

    public void keyPressed( int key )
    {
        if ( (System.currentTimeMillis()-lastPress)<500 )
        {
            pl.vibrate();
            keyPress++;
            if (keyPress==2) pl.endScreenSaver();
        }
        else
            keyPress=0;
        lastPress = System.currentTimeMillis();
    }
}
